<template>
    <div>
        <el-dialog
                title="器材维修登记"
                :visible.sync="fixShow"
                :show-close="false"
                :before-close="handleClose"
                @open="onOpen">
            <div style="height: 260px">
                <el-form label-position="right" :rules="rules" :model="OverhaulForm" ref="ruleForm" label-width="86px">
                    <el-form-item label="器材名称">
                        <el-input v-model="OverhaulForm.name" disabled/>
                    </el-form-item>
                    <el-form-item label="检修员工">
                        <el-select v-model="OverhaulForm.employeeId" placeholder="请选择">
                            <el-option
                                    v-for="item in MaintainerList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="维修数量">
                        <el-input-number v-model="OverhaulForm.count" :min="1" :max="fixEquip.count"/>
                    </el-form-item>
                    <el-form-item label="维修价格" prop="price">
                        <el-input v-model="OverhaulForm.price" placeholder="请输入价格"></el-input>
                    </el-form-item>
                </el-form>
                <div style="float: right;">
                    <el-button type="primary" size="mini" icon="el-icon-upload2" @click="fixSubmit">提交</el-button>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {getAllMaintainer} from '@/utils/apis/employee'
    import {saveOverhaul} from '@/utils/apis/equip'

    export default {
        name: "GoFix",
        data() {
            return {
                rules: {
                    price: [
                        {
                            required: true, validator: (rule, value, callback) => {
                                if (!value) return callback(new Error('请输入金额'))
                                if (!Number(value)) return callback(new Error('请输入数字值'))
                                // let reg = /^(([1-9]{1}\d*)|(0{1}))(\.\d{0,2})$/g; //小数限制0-2位
                                let reg = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;
                                if (!reg.test(value)) return callback(new Error('请输入正确价格'));
                                callback();
                            }, trigger: 'blur'
                        },
                    ]
                },
                //维修单
                OverhaulForm: {
                    employeeId: 0,
                    count: 1,
                    price: 0.0,
                },
                //检修员列表
                MaintainerList: [],

            }
        },
        props: ['fixShow', 'fixEquip'],
        methods: {
            //打开窗口的回调
            onOpen() {
                this.initData()
            },
            async initData() {
                await getAllMaintainer().then(res => {
                    //后端修改后删除data后的[0]
                    this.MaintainerList = res.data.data
                })
                const obj = JSON.parse(JSON.stringify(this.fixEquip))
                this.OverhaulForm.name = obj.name
                console.log(this.MaintainerList)
                this.OverhaulForm.employeeId = this.MaintainerList[0].id
            },
            handleClose() {
                this.cancel()
            },
            cancel() {
                this.OverhaulForm = {
                    employeeId: 0,
                    count: 1,
                    price: 0.0
                }
                this.$emit('cancel')
                this.$notify.info('取消添加记录')
            },
            fixSubmit() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        console.log(this.OverhaulForm)
                        saveOverhaul(this.OverhaulForm).then(res => {
                            this.$message.success(res.data.data)
                            this.$emit('cancel')
                        })
                    }
                })
            },
        }
    }
</script>

<style scoped>

</style>